package com.east.config.handler;

import cn.hutool.json.JSONUtil;
import com.east.entity.User;
import com.east.result.Result;
import com.east.utils.CodeEnum;
import com.east.utils.Constants;
import com.east.utils.ResponseUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Component
public class CustomLogoutSuccessHandler implements LogoutSuccessHandler {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        String userId = request.getParameter("id");
        // 移除redis中的jwt
        stringRedisTemplate.delete(Constants.REDIS_JWT_KEY+userId);

        String logoutOkStr = JSONUtil.toJsonStr(Result.OK(CodeEnum.LOGOUT_OK));
        ResponseUtils.write(response,logoutOkStr);
        System.out.println("退出成功");
    }
}
